//	开始注册--模态框
// $(".register").click(function(){
// 	$("#register_modal").modal({
// 		keyboard:true,	//	按下Escape键是否关闭模态框
// 		backdrop:false	//	点击模态框之外是否关闭模态框
// 	});
// })

$("#register_modal").on('shown.bs.modal',function(){
	$("#register_account").focus();
	//twitter bootstrap script
	$("input#register_account").blur(function(){
	$.ajax({
		type: "POST",
		url: "/ml/user_registration/",
		data: {name:$('#register_account').val(),type:'first'},
	success: function(data){
		var status=data.user_exist_status;
		var account_value=data.account;
		$('#register_account').addClass('failure');
		if (account_value==""){
			$("#ac_error").html("工號不能爲空!");
			$("#ac_error").show();
		}else if(account_value.length>0&&account_value.length>9){
			$("#ac_error").html("工號長度不能超過8位");
			$("#ac_error").show();
		}
		else if(status==false){
			$("#ac_error").html("该用户已经存在!");
			$("#ac_error").show();
		}else{
			$("#ac_error").hide();
			$('#register_account').removeClass('failure');
		}
		},
	error: function(){
		alert("failure");
	 }
		});
	 });
	$("input#register_email").blur(function(){
		$.ajax({
			type: "POST",
			url: "/ml/user_registration/",
			data: {email:$('#register_email').val(),type:'two'},
		success: function(data){
			var status=data.user_exist_status;
			var email_value=data.email;
			$('#register_email').addClass('failure');
			if (email_value==""){
				$("#email_error").html("邮箱不能为空");
				$("#email_error").show();
			}
			else if(status==false){
				$("#email_error").html("该邮箱已存在");
				$("#email_error").show();
			}else{
				$("#email_error").hide();
				$('#register_email').removeClass('failure');
			}
			},
		error: function(){
			alert("failure");
		 }
			});
		 });
	$("#register_btn").click(function(){
		$.ajax({
			type: "POST",
			url: "/ml/user_registration/",
			data: {name:$('#register_account').val(),password:$('#register_password1').val(),email:$('#register_email').val(),type:'there'},
		success: function(data){
			var status=data.submit_status;
			var all_input=$('#form1 input');
			var user=data.user;
			if (all_input.hasClass('failure' || status==false)){
				alert('请检查你的输入项');
			}else {
				$('.login').removeAttr('data-target');
				$('.login a').text(user);
				$('.register').removeAttr('data-target');
				$('.register a').text('注销').attr('href','/ml/userLogout/');
				$('#register_modal').modal('hide');
			}
			},
		error: function(){
			alert("failure");
		 }
			});
		 });
})









$('#login_modal').on('show.bs.modal',function () {
	$("#login").click(function(){
		$.ajax({
			type: "POST",
			url: "/ml/user_login/",
			data: {name:$('#login_account').val(),password:$('#login_password').val(),remember_me:$('#remember:checked').val()},
		success: function(data){
				var ms=data.ms;
				var login_status=data.login_status;
				var user=data.user;
				var password=data.ps;
				if (login_status==false) {
                    alert(ms);
                }else{
					$('.login').removeAttr('data-target');
					$('.login a').text(user);
					$('.register').removeAttr('data-target');
					$('.register a').text('注销').attr('href','/ml/userLogout/');
					$('#login_modal').modal('hide');
				}
			},
		error: function(){
			alert("failure");
		 }
			});
		 });
})





















//	隐藏注册模态框，清空模态框里的值
$("#register_modal").on('hidden.bs.modal',function(){	//	当模态框完全对用户隐藏时触发
	$("#register_modal input").each(function(){
		$(this).val('');
		$(this).attr("style","border-color:#CCCCCC;");
	});
	$(".register_error").hide();
})


//	注册工号验证
// function check_workid(){
// 	var id_num = document.getElementById("register_account").value;
// 	if (id_num.length>0) {
// 		if (id_num.length<9) {
// 			$("#workid_error").hide();
// 			$("#register_account").attr("style","border-color:#cccccc;");
// 		} else{
// 			$("#register_account").attr("style","border-style:solid; border-color:red; border-width: thin; border-radius: 3px;");
// 			$("#workid_error").html("工號長度不能超過8位");
// 			$("#workid_error").show();
// 		}
// 	} else{
// 		$("#register_account").attr("style","border-style:solid; border-color:red; border-width: thin; border-radius: 3px;");
// 		$("#workid_error").html("工號不能爲空");
// 		$("#workid_error").show();
// 	}
// }


//	注册密码验证
function check_pwd1(){
	var pwd1 = document.getElementById("register_password1").value;
	var pwd2 = document.getElementById("register_password2").value;
	if (pwd1.length>0) {
		if (pwd2.length>0) {
			if (pwd1 == pwd2) {
				$("#pwd1_error").hide();
				$("#register_password1").attr("style","border-color:#CCCCCC;").removeClass('failure');
				$("#pwd2_error").hide();
				$("#register_password2").attr("style","border-color:#CCCCCC;").removeClass('success');
			} else{
				$("#register_password1").attr("style","border-style:solid; border-color:red; border-width:thin; border-ridius:3px;").addClass('failure');
				$("#register_password2").attr("style","border-style:solid; border-color:red; border-width:thin; border-ridius:3px;").addClass('failure');
				$("#pwd2_error").html("兩次輸入的密碼不一致，請重新輸入");
				$("#pwd2_error").show();
			}
		} else{
			$("#pwd1_error").hide();
			$("#register_password1").attr("style","border-color:#CCCCCC").addClass('failure');
//			$("#pwd2_error").hide();
		}
	} else{
		$("#register_password1").attr("style","border-style:solid; border-color:red; border-width: thin; border-ridius: 3px;").addClass('failure');
		$("#pwd1_error").html("密碼不能爲空");
		$("#pwd1_error").show();
	}
}

function check_pwd2(){
	var pwd1 = document.getElementById("register_password1").value;
	var pwd2 = document.getElementById("register_password2").value;
	if (pwd2.length>0) {
		if (pwd1.length>0) {
			if (pwd1 == pwd2) {
				$("#pwd1_error").hide();
				$("#register_password1").attr("style","border-color:#CCCCCC;").removeClass('failure');
				$("#pwd2_error").hide();
				$("#register_password2").attr("style","border-color:#CCCCCC;").removeClass('failure');
			} else{
				$("#register_password1").attr("style","border-style:solid; border-color:red; border-width:thin; border-ridius:3px;");
				$("#register_password2").attr("style","border-style:solid; border-color:red; border-width:thin; border-ridius:3px;");
				$("#pwd2_error").html("兩次輸入的密碼不一致，請重新輸入");
				$("#pwd2_error").show();
			}
		} else{
			$("#pwd2_error").hide();
			$("#register_password2").attr("style","border-color:#CCCCCC").addClass('success');
//			$("#pwd2_error").hide();
		}
	} else{
		$("#register_password2").attr("style","border-style:solid; border-color:red; border-width:thin; border-ridius:3px;").addClass('failure');
		$("#pwd2_error").html("密碼不能爲空");
		$("#pwd2_error").show();
	}
}


//	注册邮箱验证
function check_email(){
	var email_val = document.getElementById("register_email").value;
	if (email_val.length>0) {
		$("#register_email").attr("style","border-color:#CCCCCC;").removeClass('failure');
		$("#email_error").hide();


	} else{
		$("#register_email").attr("style","border-style:solid; border-color:red; border-width:thin; border-ridius:3px;").addClass('failure');
		$("#email_error").html("郵箱不能爲空");
		$("#email_error").show();
	}
}

        // $("#register_btn").click(function(){
        // /*	var id_num = document.getElementById("register_account").value;
        //     var pwd1 = document.getElementById("register_password1").value;
        //     var pwd2 = document.getElementById("register_password2").value;
        //     var email_val = document.getElementById("register_email").value;*/
        //    if ($("#register_account").val() != '' && $("#register_password1").val() != '' && $("#register_password2").val() != '' && $("#register_email").val() != '') {
        //         alert("成功注册");
        //     } else{
        //         alert("你的輸入有誤，請檢查!");
        //         return false;
        //     }
        // })
